<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;
use Admin\Model\LandModel;

class ShopController extends AuthController {

    protected $limit = 10;
    protected $recommends_arr = array(
        1 => '热销',
        2 => '道具',
        3 => '宝箱',
        4 => '宠物'
    );
    protected $com_type_arr = array(
        'crops' => '果实',
        'material' => '材料',
        'gem' => '宝石',
        'prop' => '道具'
    );

    //商品列表
    public function shop_commodity_list() {
//        $lands=D('CropsInfo');
//        $data=$lands->selectCrops();
        $count = M('shop_commodity')->count();
        $Page = new \Think\Page($count, $limit);
        $show = $Page->show(); // 分页显示输出
        $commoditys = M('shop_commodity')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        //格式化数据
        foreach ($commoditys as $key => $val) {
            $commoditys[$key]['com_type']=$this->com_type_arr[$val['com_type']];
            $recommends = makeSemiangle($val['com_recommend']);
            $recommends = explode(',', $recommends);
            $recommends = empty($recommends[0]) ? '' : $recommends;
            foreach ($recommends as $k => $v) {
                $recom_arr[] = $this->recommends_arr[$v];
            }
            $commoditys[$key]['com_recommend'] = implode(" ", $recom_arr);
        }

        $this->assign('commoditys', $commoditys);
        $this->assign('page', $show);
        $this->display();
    }

    public function shop_commodity_add() {
        if (!IS_POST) {
            $this->display();
        } else {
            $checkbox = I('form-field-checkbox');
            $recommend = empty($checkbox) ? '' : implode(',', $checkbox);
            $data = array(
                'com_name' => I('com_name'),
                'com_type' => I('com_type'),
                'com_count' => I('com_count'),
                'com_price' => I('com_price'),
                'com_sum' => I('com_sum'),
                'com_img' => I('uploader_file'),
                'com_describe' => I('com_describe'),
                'com_recommend' => $recommend,
                'display' => I('display'),
                'add_time' => time(),
            );
            M('shop_commodity')->add($data);
            $this->success('商品保存成功', U('shop_commodity_list'), 1);
        }
    }

    public function shop_commodity_edit() {
        if (!IS_POST) {
            $id = I('id');
            $commoditys = M('shop_commodity');
            $commoditys = $commoditys->where(array('id' => $id))->find();
            $commoditys['com_recommend'] = explode(',', $commoditys['com_recommend']);
            $this->assign('commoditys', $commoditys);
            $this->display();
        } else {
            $checkbox = I('form-field-checkbox');
            $recommend = empty($checkbox) ? '' : implode(',', $checkbox);
            $data = array(
                'com_name' => I('com_name'),
                'com_type' => I('com_type'),
                'com_count' => I('com_count'),
                'com_price' => I('com_price'),
                'com_sum' => I('com_sum'),
                'com_img' => I('uploader_file'),
                'com_describe' => I('com_describe'),
                'com_recommend' => $recommend,
                'display' => I('display')
            );
            M('shop_commodity')->where('id=' . I('edit_id'))->save($data);
            $this->success('商品修改成功', U('shop_commodity_list'), 1);
        }
    }

    //删除农作物
    public function shop_commodity_del() {
        $column = M('shop_commodity')->where(array('id' => I('del_id')))->delete();
        $this->redirect('shop_commodity_list');
    }

    public function upimage() {
        return ajaxUploader();
    }

}
